package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.model.Topic;
import com.cskaoyan.model.TopicExample;
import com.cskaoyan.model.bo.TopicListBO;
import com.cskaoyan.model.vo.*;
import com.cskaoyan.service.TopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: TopicServiceImpl
 * @Description: topic模块实现类
 * @Author:YPS
 * @Date: 2022/5/6 17:46
 * @Version: 1.0
 **/
@Service
@Transactional
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;

    @Override
    public DataVO<Topic> queryTopic(TopicListBO topicListBO) {
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        topicExample.setOrderByClause(topicListBO.getSort() + " " + topicListBO.getOrder());
        if (!(topicListBO.getTitle()==null||"".equals(topicListBO.getTitle()))){
            criteria.andTitleLike("%" + topicListBO.getTitle() + "%");
        }
        if (!(topicListBO.getSubtitle()==null||"".equals(topicListBO.getSubtitle()))){
            criteria.andSubtitleLike("%" + topicListBO.getSubtitle() + "%");
        }
        PageHelper.startPage(topicListBO.getPage(), topicListBO.getLimit());
        List<Topic> dataVO = topicMapper.selectByExampleWithBLOBs(topicExample);
        PageInfo<Topic> pageInfo = new PageInfo<>(dataVO);
        Long total = pageInfo.getTotal();
        DataVO<Topic> topicDataVO = new DataVO<>();
        System.out.println(dataVO.get(0).getContent());
        topicDataVO.setList(dataVO);
        topicDataVO.setLimit(topicListBO.getLimit());
        topicDataVO.setPage(topicListBO.getPage());
        topicDataVO.setTotal(Math.toIntExact(total));
        topicDataVO.setPages(pageInfo.getPages());
        return topicDataVO;
    }

    @Override
    public Topic createTopic(Topic topic) {
        System.out.println(topic.getPicUrl());
        Date date = new Date();
        topic.setAddTime(date);
        topic.setDeleted(false);
        topicMapper.insertSelective(topic);
        return topic;
    }

    @Override
    public TopicReadVO readTopic(Integer id) {
        TopicReadVO topicRead = new TopicReadVO();
        TopicTopicVO topicReadVO = new TopicTopicVO();
        Topic topic = topicMapper.selectByPrimaryKey(id);
        List<TopicGoodsVO> topicGoodsVOS = new ArrayList<>();
        topicReadVO.setAddTime(topic.getAddTime());
        topicReadVO.setContent(topic.getContent());
        topicReadVO.setDeleted(topic.getDeleted());
        topicReadVO.setGoods(topic.getGoods());
        topicReadVO.setId(topic.getId());
        topicReadVO.setPicUrl(topic.getPicUrl());
        topicReadVO.setPrice(topic.getPrice());
        topicReadVO.setReadCount(topic.getReadCount());
        topicReadVO.setSortOrder(topic.getSortOrder());
        topicReadVO.setSubtitle(topic.getSubtitle());
        topicReadVO.setTitle(topic.getTitle());
        topicReadVO.setUpdateTime(new Date());
        for (Integer good : topic.getGoods()) {
            TopicGoodsVO topicGoodsVO = topicMapper.selectGoodsById(good);
            topicGoodsVOS.add(topicGoodsVO);
        }
        topicRead.setGoodsList(topicGoodsVOS);
        topicRead.setTopic(topicReadVO);
        return topicRead;

    }

    @Override
    public Topic updateTopic(Topic topic) {
        Date date = new Date();
        topic.setUpdateTime(date);
        topicMapper.updateByPrimaryKeySelective(topic);
        return topic;
    }

    @Override
    public void deleteTopic(Topic topic) {
        topic.setUpdateTime(new Date());
        topic.setDeleted(true);
        topicMapper.updateByPrimaryKeySelective(topic);
    }

    @Override
    public void batchDelete(Integer[] ids) {
        for (Integer id : ids) {
            Topic topic = new Topic();
            topic.setId(id);
            topic.setDeleted(true);
            topicMapper.updateByPrimaryKeySelective(topic);
        }
    }

}
